package com.kuaiyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaiyin.dto.EnvironmentDTO;
import com.kuaiyin.entity.Environment;
import com.kuaiyin.enums.BizCodeEnum;
import com.kuaiyin.exception.BaseException;
import com.kuaiyin.mapper.EnvironmentMapper;
import com.kuaiyin.service.IEnvironmentService;
import com.kuaiyin.utils.PageHelper;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.utils.RedisUtils;
import com.kuaiyin.vo.EnvironmentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * <p>
 * 全局变量表 服务实现类
 * </p>
 *
 * @author ng
 * @since 2025-04-27
 */
@Service
public class EnvironmentServiceImpl extends ServiceImpl<EnvironmentMapper, Environment> implements IEnvironmentService {
    @Autowired
    private EnvironmentMapper environmentMapper;
    @Autowired
    private RedisUtils redisUtils;

    // 提取 redisKey 为常量
    private static final String ENV_REDIS_KEY_PREFIX = "environment:";

    @Override
    public void saveEnvironment(EnvironmentDTO environmentDTO) {
        String varName = environmentDTO.getVarName();
        String envType = environmentDTO.getEnvironmentType();

        // 1. 校验 Redis 缓存中是否存在相同变量名
        Map<String, String> cachedVars = redisUtils.getHashEntriesAsStringMap(ENV_REDIS_KEY_PREFIX + envType);

        if (cachedVars != null && cachedVars.containsKey(varName)) {
            throw BaseException.of(BizCodeEnum.ENVIRONMENT_VAR_EXIST_IN_CACHE);
        }
        //2、存入缓存
        redisUtils.hSet(ENV_REDIS_KEY_PREFIX + envType, varName, environmentDTO.getVarValue());

        // 3. 插入数据库
        Environment environment = new Environment();
        BeanUtils.copyProperties(environmentDTO, environment);
        environmentMapper.insert(environment);
    }

    @Override
    public void updateEnvironment(Long id, EnvironmentDTO environmentDTO) {
        environmentDTO.setId(id);
        //0、 校验名称是否已存在
        if (lambdaQuery().eq(Environment::getVarName, environmentDTO.getVarName()).ne(Environment::getId, id).exists()) {
            throw BaseException.of(BizCodeEnum.ENVIRONMENT_VAR_NAME_EXISTS);
        }
        //1。更新数据库
        Environment environment = new Environment();
        BeanUtils.copyProperties(environmentDTO, environment);
        environmentMapper.updateById(environment);
        //2、更新缓存
        redisUtils.hSet(ENV_REDIS_KEY_PREFIX + environmentDTO.getEnvironmentType(), environmentDTO.getVarName(), environmentDTO.getVarValue());
    }

    @Override
    public void removeById(Long id) {
        Environment environment = super.getById(id);
        super.removeById(id);
        redisUtils.hDel(ENV_REDIS_KEY_PREFIX + environment.getEnvironmentType(), environment.getVarName());
    }

    @Override
    public EnvironmentVO getById(Long id) {
        Environment environment = super.getById(id);
        EnvironmentVO environmentVO = new EnvironmentVO();
        BeanUtils.copyProperties(environment, environmentVO);
        return environmentVO;
    }

    @Override
    public List<EnvironmentVO> getEnvironmentList() {
        return PageHelper.queryAllWithVo(environmentMapper, EnvironmentVO.class);
    }

    @Override
    public PageResult<EnvironmentVO> getEnvironmentPage(Integer page, Integer size, Consumer<LambdaQueryWrapper<Environment>> condition) {
        return PageHelper.queryWithVo(page, size, environmentMapper, EnvironmentVO.class, condition);
    }
}
